package org.dromara.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.CriticalAlarmRiskEventBo;
import org.dromara.system.domain.vo.CriticalAlarmRiskEventVo;
import org.dromara.system.service.ICriticalAlarmRiskEventService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 关键报警风险事件
 *
 * @author Lion Li
 * @date 2025-10-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/alarmRiskEvent")
public class CriticalAlarmRiskEventController extends BaseController {

    private final ICriticalAlarmRiskEventService criticalAlarmRiskEventService;

    /**
     * 查询关键报警风险事件列表
     */
    @SaCheckPermission("system:alarmRiskEvent:list")
    @GetMapping("/list")
    public TableDataInfo<CriticalAlarmRiskEventVo> list(CriticalAlarmRiskEventBo bo, PageQuery pageQuery) {
        return criticalAlarmRiskEventService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取关键报警风险事件导入模板
     */
    @SaCheckPermission("system:alarmRiskEvent:downloadTemplate")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        criticalAlarmRiskEventService.downloadTemplate(response);
    }

    /**
     * 导入数据
     */
    @SaCheckPermission("system:alarmRiskEvent:import")
    @PostMapping("/importData")
    public R<Boolean> importData(@RequestParam MultipartFile file) {
        return R.ok(criticalAlarmRiskEventService.importData(file));
    }

    /**
     * 导出关键报警风险事件列表
     */
    @SaCheckPermission("system:alarmRiskEvent:export")
    @Log(title = "关键报警风险事件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CriticalAlarmRiskEventBo bo, HttpServletResponse response) {
        List<CriticalAlarmRiskEventVo> list = criticalAlarmRiskEventService.queryList(bo);
        ExcelUtil.exportExcel(list, "关键报警风险事件", CriticalAlarmRiskEventVo.class, response);
    }

    /**
     * 获取关键报警风险事件详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:alarmRiskEvent:query")
    @GetMapping("/{id}")
    public R<CriticalAlarmRiskEventVo> getInfo(@NotNull(message = "主键不能为空")
                                               @PathVariable Long id) {
        return R.ok(criticalAlarmRiskEventService.queryById(id));
    }

    /**
     * 新增关键报警风险事件
     */
    @SaCheckPermission("system:alarmRiskEvent:add")
    @Log(title = "关键报警风险事件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CriticalAlarmRiskEventBo bo) {
        return toAjax(criticalAlarmRiskEventService.insertByBo(bo));
    }

    /**
     * 修改关键报警风险事件
     */
    @SaCheckPermission("system:alarmRiskEvent:edit")
    @Log(title = "关键报警风险事件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CriticalAlarmRiskEventBo bo) {
        return toAjax(criticalAlarmRiskEventService.updateByBo(bo));
    }

    /**
     * 删除关键报警风险事件
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:alarmRiskEvent:remove")
    @Log(title = "关键报警风险事件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(criticalAlarmRiskEventService.deleteWithValidByIds(List.of(ids), true));
    }
}
